package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/service"
	"log"
	"os"
	"strings"
	"wave-service/server/app/common/im"

	"wave-service/server/app/internal/config"
	"wave-service/server/app/internal/handler"
	"wave-service/server/app/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

func main() {
	workDir, _ := os.Getwd()
	content, err := os.ReadFile(workDir + "/.env")
	if err != nil {
		fmt.Println("Fatal error .env file: ", err)
	}
	contentText := string(content)
	// 去除空格
	contentText = strings.Replace(contentText, " ", "", -1)
	// 去除换行符
	contentText = strings.Replace(contentText, "\n", "", -1)
	arr := strings.Split(contentText, "=")

	configName := "app-api"
	if arr[1] == "docker" {
		configName = "app-api-docker"
	}
	fileName := fmt.Sprintf("etc/%v.yaml", configName)
	log.Println("configFileName=" + fileName)

	var configFile = flag.String("f", fileName, "the config file")

	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	ctx := svc.NewServiceContext(c)
	server := rest.MustNewServer(c.RestConf, rest.WithCors())
	//defer server.Stop()

	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	//server.Start()

	group := service.NewServiceGroup()
	defer group.Stop()
	group.Add(server)
	group.Add(im.Server{SvcCtx: ctx})
	group.Start()

}
